package com.xcity.game.guild.operation;

import com.xcity.db.entity.GuildMember;
import com.xcity.db.entity.Player;
import com.xcity.db.mapper.GuildApplyMapper;
import com.xcity.db.mapper.GuildMapper;
import com.xcity.db.mapper.GuildMemberMapper;
import com.xcity.game.common.AbstractConfirmOperation;
import com.xcity.game.common.MessageConst;
import com.xcity.game.guild.GuildService;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.QuitGuildResponse;
import com.xcity.util.DBUtils;

import naga.x.App;

/**
 * 解散公会确认操作
 */
public class GuildDisbandOperation extends AbstractConfirmOperation{

	protected GuildMember member;
	protected int serial;
	protected long guildId;
	
	public GuildDisbandOperation(Player ref, GuildMember member, long guildId, int serial, int timeoutMillis, String msg) {
		super(ref, timeoutMillis, msg);
		this.member = member;
		this.serial = serial;
		this.guildId = guildId;
	}

	@Override
	public void ok(Player player) {
		GuildService gs = App.getApp().getServiceManager().get(GuildService.class);

		if(member != null){
			if(member.getGuildid() == guildId){
				if(member.getDuty() == GuildService.MEMBER_DUTY_TYPE_CHAIRMAN){
					int count = DBUtils.select(GuildMemberMapper.STATEMENT_COUNT_MEMBER_BY_GUILD, member.getGuildid());
					if(count > 1){
						player.send(new HintResponse(serial, MessageConst.GUILD_OPERATION_TOO_LATE));
						return;
					}
					gs.removeGuildByGuildId(member.getGuildid());
					DBUtils.delete(GuildMapper.STATEMENT_DELETE_GUILD_BY_GUILD, member.getGuildid());
					member.reset();
					gs.removeGuildChannel(guildId);
					DBUtils.update(member, GuildMemberMapper.class);
					player.send(new QuitGuildResponse(serial, 0));
					DBUtils.delete(GuildApplyMapper.STATEMENT_DELETE_BY_GUILDID, this.guildId);
					gs.toDisbandLog(player.getId(), guildId);
					return;
				}
			}
		}
		player.send(new HintResponse(serial, MessageConst.GUILD_OPERATION_TOO_LATE));
	}
	
	@Override
	public void cancel(Player player){
		//取消
		player.send(new HintResponse(serial, MessageConst.GUILD_OPERATION_CANCEL));
	}
}
